/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb;

/**
 * <p> Extension component for {@link syntelos.iou.cloader IOU
 * ClassLoader}.
 * </p>
 * 
 * @author jdp
 */
public final class Compiler 
    extends java.lang.Object
    implements syntelos.iou.cloader.Compiler
{
    public final static java.lang.String FILENAME_SUFFIX = "xb";

    public Compiler(){
        super();
    }

    public java.lang.String getFilenameExtension(){
        return FILENAME_SUFFIX;
    }
    public void compile(syntelos.iou.cloader caller,
                        java.lang.String name,
                        java.net.URL source_url,
                        java.io.InputStream source,
                        java.io.OutputStream target)
        throws java.io.IOException
    {
        //
        IO.Uri src = new IO.Uri(source_url.toExternalForm());
        /*
         * Read XML
         */
        Class xclass = Class.readXB(src,source);
        /*
         * Compile tree
         */
        syntelos.iou.dom.rw.Process compiler = 
            new syntelos.iou.dom.rw.Process(xclass);
        syntelos.iou.dom.rw.Context context =
            new syntelos.iou.dom.rw.Context.Document();

        while (compiler.compile(context));

        //
        IO.Uri dst = new IO.Uri(syntelos.iou.chbuf.cat("buffer:",this.getClass().getName()));
        CF.Writer writer = new CF.Writer.Stream(target);
        /*
         * Write JCF
         */
        xclass.writeCF(dst,writer);
    }
}
